<?php

declare(strict_types=1);

namespace Modules\Withdraw\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Illuminate\Http\Request;
use Modules\Member\Models\Members;
use Modules\Member\Models\MembersPriceLog;
use Modules\Member\Models\MembersWithdraw;
use Modules\Order\Models\Orders;

/**
 * 订单控制器
 */
class WithdrawController extends Controller
{
    public function __construct(
        protected readonly MembersWithdraw $model
    ) {
    }

    public function index(): mixed
    {
        return $this->model->setBeforeGetList(function ($query){
            return $query->with('user');
        })->getList();
    }

    /**
     * @return mixed
     */
    public function store(Request $request)
    {
        $params = $request->all();
        return $this->model->storeBy($params);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function show($id): mixed
    {
        return $this->model->firstBy($id);
    }

    /**
     * @param $id
     * @param Request $request
     * @return mixed
     */
    public function update($id, Request $request): mixed
    {
        $params = $request->all();
        $info = $this->model->find($id);
        $status = $params['status']-1;
        if($status ==2 && Members::where('id',$info['user_id'])->increment('price',$info->money)){   //拒绝金额退回用户
            //增加余额变动日志
            $priceLog = [];
            $priceLog = new MembersPriceLog();
            $priceLog->user_id = $info->user_id;
            $priceLog->money = $info->money;
            $priceLog->type = 1;
            $priceLog->status = 1;
            $priceLog->save();
        }else{
            Members::where('id',$info['user_id'])->increment('withdraw_all',$info->money);
        }
        return $this->model->updateBy($id, $request->all());
    }

    /**
     * @param $id
     * @return mixed
     */
    public function destroy($id): mixed
    {
        return $this->model->deleteBy($id);
    }

    /**
     * @param $id
     * @return bool
     */
    public function enable($id)
    {
        return $this->model->toggleBy($id);
    }

    public function export(Request $request): mixed
    {
        $status = $request->get('status');
        $user_id = $request->get('user_id');
        $account = $request->get('account');
        $real_name = $request->get('real_name');
        $start_time = $request->get('start_time');
        $end_time = $request->get('end_time');
        $sql =  $this->model
            ->select('user_id', 'account', 'real_name','money','status','created_at');
        if(!empty($user_id)){
            $sql->where('user_id', $user_id);
        }
        if(!empty($account)){
            $sql->where('account', $account);
        }
        if(!empty($real_name)){
            $sql->where('real_name', $real_name);
        }
        if (!empty($status)) {
            $sql->where('status', $status);
        }
        if (!empty($start_time) && !empty($end_time)) {
            $start_time = strtotime($start_time);
            $end_time = strtotime($end_time);
            $sql->whereBetween('created_at', [$start_time, $end_time]);
        }
        $sql->where('type', 1);
        $list = $sql->get();
        return $list->setHidden(['status','created_at'])->download(['用户id', '账号', '真实姓名','金额', '提现状态']);

    }
}
